package com.leetcode.August;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 438. 找到字符串中所有字母异位词
 * @Author: Mr.Fxy
 * @CreateTime: 2024-08-18 22:28
 */
public class LC438找到字符串中所有字母异位词 {

    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> result = new ArrayList<>();

        if (s == null || s.length() == 0 || p == null || p.length() == 0 || s.length() < p.length()) {
            return result;
        }
        char[] sorted = p.toCharArray();
        Arrays.sort(sorted);
        for (int i = 0; i < s.length() - sorted.length; i++) {
            char[] chars = s.substring(i, i + sorted.length).toCharArray();
            Arrays.sort(chars);
            if (Arrays.equals(chars, sorted)) {
                result.add(i);
            }
        }
        return result;
    }
}
